const obj = {
  data: {
    a: 1,
    b: {
      c: 1,
    },
  },
  watch: {
    a(val, oldVal) {
      console.log(val, oldVal)
    },
    b(val, oldVal) {
      console.log(val, oldVal)
    },
  },
}
 
/**
 * 
const instance = watch(obj)
 
instance.a = 2 // log => 2 1
instance.b.c = 2 // log => { c: 2 } { c: 1 }
实现watch函数；
 */

function watch (obj) {
  return new Proxy(obj.data, {
    get: (target, key) => {
      return key in target ? target[key]
                           : "error"
    },
    set: (target, key, value) => {
      target[key] = value
    }
  })
}

const instance = watch(obj)
console.log(instance.a)
instance.b.c = 2
console.log(instance.b.c)
console.log(obj.data.b.c)
